package com.groupseven.backend.controller;

import com.groupseven.backend.domain.Assessment.AssessmentItem;
import com.groupseven.backend.domain.Assessment.AssessmentPlan;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/assessment")
public class AssessmentController {

    @Autowired
    private AssessmentService assessmentService;
    @Autowired
    private ProgramService programService;
    @Autowired
    private PaperService paperService;
    @Autowired
    private PatentService patentService;
    @Autowired
    private PrizeProductService prizeProductService;

    /**
     * @Author 王志华
     * @return
     */
    @GetMapping("")
    public ResultType getAllPlan(){
        List<AssessmentItem> items = assessmentService.getAllAssessmentItems();
        List<AssessmentPlan> plans = assessmentService.getAllAssessmentPlans();
        for(AssessmentPlan PIterator : plans){
            List<AssessmentItem> tmp = new ArrayList<>();
            for(AssessmentItem IIterator : items){
                if(IIterator.getBelonging()==PIterator.getPlanid()){
                    tmp.add(IIterator);
                }
            }
            PIterator.setPlan(tmp);
        }
        ResultType result = new ResultType(1,"查询考核计划成功！",200);
        result.setPlans(plans);
        return result;
    }

    /**
     * @Author 王志华
     * @param Assessmentid
     * @return
     */
    @DeleteMapping("/item/{Assessmentid}")
    public ResultType deleteAssessmentItemById(@PathVariable int Assessmentid){
        if(assessmentService.deleteAssessmentItemById(Assessmentid)==1){
            return new ResultType(1,"删除考核项成功！",200);
        }
        return new ResultType(0,"删除考核项失败！",200);
    }

    /**
     * @Author 王志华
     * @param Planid
     * @return
     */
    @DeleteMapping("/plan/{Planid}")
    public ResultType deleteAssessmentPlanById(@PathVariable int Planid){
        if(assessmentService.deleteAssessmentPlanById(Planid)==1){
            return new ResultType(1,"删除考核计划成功！",200);
        }
        return new ResultType(0,"删除考核计划失败！",200);
    }

    /**
     * @Author 王志华
     * @param item
     * @return
     */
    @PostMapping("/item")
    public ResultType addItemForPlanByPlanid(@RequestBody AssessmentItem item){
        if(assessmentService.checkAddForPlanItem(item)) {
            if (assessmentService.addItemForPlanByPlanid(item) == 1) {
                return new ResultType(1, "增加考核项成功！", 200);
            }
            return new ResultType(0, "增加考核项失败！", 200);
        }
        return new ResultType(0,"考核类型与等级不可重复！",200);
    }

    /**
     * @Author 王志华
     * @param item
     * @return
     */
    @PutMapping("/item")
    public ResultType updateItem(@RequestBody AssessmentItem item){
        if(assessmentService.updateAssessmentItem(item)){
            return new ResultType(1,"修改考核项成功！",200);
        }
        return new ResultType(0,"修改考核项失败！",200);
    }

    /**
     * @Author 王志华
     * @param Username
     * @param Planid
     * @return
     */
    @GetMapping("/result/{Username}/{Planid}")
    public ResultType getAssessmentScoreByUsernameAndPlanid(@PathVariable String Username, @PathVariable int Planid){
        ResultType result = new ResultType(1,"查询成功！",200);
        result.setSingleAssessmentResult(assessmentService.getAssessmentScoreByUsernameAndPlanid(Username,Planid));
        return result;
    }

    @GetMapping("/result/{Planid}")
    public ResultType getAssessmentScoreByPlanid(@PathVariable int Planid){
        ResultType result = new ResultType(1,"查询成功！",200);
        result.setMultiAssessmentResult(assessmentService.getAssessmentScoreByPlanid(Planid));
        return result;
    }
    @GetMapping("/results/{Planid}/{StartTime}")
    public ResultType getAssessmentScoreByPlanidAndStartTime(@PathVariable int Planid, @PathVariable Date StartTime){
        System.out.println(StartTime);
        ResultType result = new ResultType(1,"查询成功！",200);
        result.setMultiAssessmentResult(assessmentService.getAssessmentScoreByPlanidAndStartTime(Planid,StartTime));
        return result;
    }
}
